/**
 * room_info 房间路由模块
 */
const express = require('express');
const router = express.Router();
const ResultJson = require('../model/ResultJson.js');
const ServiceFactory = require('../factory/ServiceFactory.js');
const ExcelUtils = require('../utils/ExcelUtils.js');

router.get('/getRoomListByName', async (req, res) => {
  let pageList = await ServiceFactory.roomInfoService.getRoomListByName(req.query);
  res.json(new ResultJson(true, '获取数据成功', pageList));
});

router.post('/addRoomInfo', async (req, res) => {
  let results = await ServiceFactory.roomInfoService.addRoomInfo(req.body);
  res.json(new ResultJson(results, results ? '新增成功' : '新增失败'));
});

router.get('/existsRoomName', async (req, res) => {
  let results = await ServiceFactory.roomInfoService.existsRoomName(req.query.room_name);
  res.send(new ResultJson(true, '数据请求成功', results));
});

router.get('/findById', async (req, res) => {
  let results = await ServiceFactory.roomInfoService.findById(req.query.id);
  res.send(new ResultJson(true, '数据请求成功', results));
});

router.post('/updataRoomInfo', async (req, res) => {
  let results = await ServiceFactory.roomInfoService.updataRoomInfo(req.body);
  res.json(new ResultJson(results, results ? '修改成功' : '修改失败'));
});

router.get('/deleteById', async (req, res) => {
  let { id } = req.query;
  let results = await ServiceFactory.roomInfoService.deleteById(id);
  res.json(new ResultJson(results, results ? '删除成功' : '删除失败'));
});

router.get('/exportExcel', async (req, res) => {
  let results = await ServiceFactory.roomInfoService.exportExcel(req.query);
  let excelPath = ExcelUtils.resultsToExcel(results);
  if (excelPath) {
    //生成好了
    res.sendFile(excelPath);
  } else {
    res.json(new ResultJson(false, 'excel没有数据，不能下载'));
  }
});

module.exports = router;
